package at.swd11.schadl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


/**
 * 
 * Show a Frame which have got three Buttons and one Label. It is possible to increase / decrease the Value, 
 * shown in the middle Label.
 * 
 * @author Patrick Schadler
 * 
 */
public class PlusMinus extends JFrame implements ActionListener {

	//Properties
	/**
	 * Contains the Actual Difference between Plus and Minus clicks
	 */
	String Erg = "0";
	/**
	 * Gets the actual Name of the Frame, when it is created
	 */
	String PlusMinusName;
	/**
	 * Contains the Actual Difference between Plus and Minus clicks 
	 */
	int i = 0;
	
	/**
	 * Fires a Event when the Plus Button was clicked
	 */
	JButton btnPlus = new JButton("Plus");
	/**
	 * Fires a Event when the Minus Button was clicked
	 */
	JButton btnMinus = new JButton("Minus");
	/**
	 * Closes the Form
	 */
	JButton btnExit = new JButton("Exit");
	
	/**
	*Shows the result on the Frame 
	*/
	JLabel lblErg = new JLabel(Erg);
	
	
	/**
	 * Sets the Default close operation, the size of the Frame and the Placement of all Controls.
	 * 
	 * @param title sets the title on the top Frame
	 */
	public PlusMinus(String title)
	{
		super(title);
		
		this.PlusMinusName = title;
			
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		btnPlus.addActionListener(this);
		btnPlus.setActionCommand("Plus");
		
		btnMinus.addActionListener(this);
		btnMinus.setActionCommand("Minus");
		
		btnExit.addActionListener(this);
		btnExit.setActionCommand("Exit");
		
		lblErg.setHorizontalAlignment(SwingConstants.CENTER);
		
		
		this.getContentPane().add(btnExit, BorderLayout.SOUTH);
		this.getContentPane().add(btnPlus, BorderLayout.EAST);
		this.getContentPane().add(btnMinus, BorderLayout.WEST);
		this.getContentPane().add(lblErg, BorderLayout.CENTER);
		this.setSize(300,300);
		this.setVisible(true);
	}
	
	
	/**
	 * Perfoms all Action Methods provided by the @see ActionListener
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
			if(e.getActionCommand() == btnExit.getActionCommand())
			{
				System.exit(0);
			}
			
			if(e.getActionCommand() == btnPlus.getActionCommand())
			{
				i++;
			}
			
			if(e.getActionCommand() == btnMinus.getActionCommand())
			{
				i--;
			}
			
			lblErg.setText(Integer.toString(i));

	}
	
	/**
	 * Returns the name of the Frame
	 * @return
	 */
	public String GetPlusMinusName()
	{
		return this.PlusMinusName;
	}

	/**
	 * Calls the PlusMinus Class.
	 * @param args no usage in the program
	 */
	public static void main(String[] args) {

		PlusMinus n = new PlusMinus("Hallo");

	}

}
